//
//  MainInterface.m
//  DaJiang
//
//  Created by sally on 16/10/12.
//  Copyright © 2016年 jiniu. All rights reserved.
//

#import "MainInterface.h"

@implementation MainInterface

//获取banner列表
- (void)queryBannerListWithCompletion:(completion_t)completion
                                error:(error_t)error {
  self.interfaceURL = [[NSURL alloc] initWithScheme:Scheme host:[ApiBase shareInstance].host path:@"/banner"];
  NSURL *url = CREATE_REAL_URL(self.interfaceURL, @"list.json");

  netCompletion_t netCompletion = ^(AFHTTPRequestOperation *operation, id responseObject) {
    NSDictionary *resultDict = responseObject;
    if (resultDict) {
      if (completion) {
        NSDictionary *resultObject = [resultDict objectForKey:DicKeyResult];
        NSInteger code = [[resultDict objectForKey:DicKeyErrorCode] integerValue];
        NSString *description = [resultDict objectForKey:DicKeyErrorMsg];
        description = description ? description : @"";
        completion(resultObject, code, description);
      }
    }
  };

  netFailure_t netFailure = ^(AFHTTPRequestOperation *option, NSError *e) {
    if (error) {
      error(e);
    }
  };

  AFHTTPRequestOperationManager *manager = [ApiBase shareInstance].shareManager;
  [manager POST:[url absoluteString] parameters:nil success:netCompletion failure:netFailure];
}

//获取地址列表
- (void)queryLocationListWithCompletion:(completion_t)completion
                                  error:(error_t)error {
  self.interfaceURL = [[NSURL alloc] initWithScheme:Scheme host:[ApiBase shareInstance].host path:@"/pro_location"];
  NSURL *url = CREATE_REAL_URL(self.interfaceURL, @"list.json");

  netCompletion_t netCompletion = ^(AFHTTPRequestOperation *operation, id responseObject) {
    NSDictionary *resultDict = responseObject;
    if (resultDict) {
      if (completion) {
        NSDictionary *resultObject = [resultDict objectForKey:DicKeyResult];
        NSInteger code = [[resultDict objectForKey:DicKeyErrorCode] integerValue];
        NSString *description = [resultDict objectForKey:DicKeyErrorMsg];
        description = description ? description : @"";
        completion(resultObject, code, description);
      }
    }
  };

  netFailure_t netFailure = ^(AFHTTPRequestOperation *option, NSError *e) {
    if (error) {
      error(e);
    }
  };

  AFHTTPRequestOperationManager *manager = [ApiBase shareInstance].shareManager;
  [manager POST:[url absoluteString] parameters:nil success:netCompletion failure:netFailure];
}

//获取一级分类列表
- (void)queryLevel1ListWithCompletion:(completion_t)completion
                                error:(error_t)error {
  self.interfaceURL = [[NSURL alloc] initWithScheme:Scheme host:[ApiBase shareInstance].host path:@"/user_pro_topic_index"];
  NSURL *url = CREATE_REAL_URL(self.interfaceURL, @"level1/list.json");

  netCompletion_t netCompletion = ^(AFHTTPRequestOperation *operation, id responseObject) {
    NSDictionary *resultDict = responseObject;
    if (resultDict) {
      if (completion) {
        NSDictionary *resultObject = [resultDict objectForKey:DicKeyResult];
        NSInteger code = [[resultDict objectForKey:DicKeyErrorCode] integerValue];
        NSString *description = [resultDict objectForKey:DicKeyErrorMsg];
        description = description ? description : @"";
        completion(resultObject, code, description);
      }
    }
  };

  netFailure_t netFailure = ^(AFHTTPRequestOperation *option, NSError *e) {
    if (error) {
      error(e);
    }
  };

  AFHTTPRequestOperationManager *manager = [ApiBase shareInstance].shareManager;
  [manager POST:[url absoluteString] parameters:nil success:netCompletion failure:netFailure];
}

//获取二级分类列表
- (void)queryLevel2ListWithLevel1Id:(NSInteger)level1Id
                         completion:(completion_t)completion
                              error:(error_t)error {
  self.interfaceURL = [[NSURL alloc] initWithScheme:Scheme host:[ApiBase shareInstance].host path:@"/user_pro_topic_index"];
  NSURL *url = CREATE_REAL_URL(self.interfaceURL, @"level2/list.json");

  NSDictionary *parameters = @{ @"level_1_id" : @(level1Id),
  };

  netCompletion_t netCompletion = ^(AFHTTPRequestOperation *operation, id responseObject) {
    NSDictionary *resultDict = responseObject;
    if (resultDict) {
      if (completion) {
        NSDictionary *resultObject = [resultDict objectForKey:DicKeyResult];
        NSInteger code = [[resultDict objectForKey:DicKeyErrorCode] integerValue];
        NSString *description = [resultDict objectForKey:DicKeyErrorMsg];
        description = description ? description : @"";
        completion(resultObject, code, description);
      }
    }
  };

  netFailure_t netFailure = ^(AFHTTPRequestOperation *option, NSError *e) {
    if (error) {
      error(e);
    }
  };

  AFHTTPRequestOperationManager *manager = [ApiBase shareInstance].shareManager;
  [manager POST:[url absoluteString] parameters:parameters success:netCompletion failure:netFailure];
}

//私人定制and悬赏请专家
- (void)privateRequestWithCompanyName:(NSString *)companyName
                             userName:(NSString *)userName
                                phone:(NSString *)phone
                              problem:(NSString *)problem
                          rewardValue:(NSString *)rewardValue
                                 time:(NSString *)time
                             location:(NSString *)location
                         locationDesc:(NSString *)locationDesc
                           completion:(completion_t)completion
                                error:(error_t)error {

  self.interfaceURL = [[NSURL alloc] initWithScheme:Scheme host:[ApiBase shareInstance].host path:@"/pro_topic"];
  NSURL *url = CREATE_REAL_URL(self.interfaceURL, @"private_request.json");
  UserModel *userModel = [[CacheManager shareInstance]getLastLoginAccount];

  NSDictionary *parameters = @{ @"uid" : @(userModel.uid),
                                @"company_name" : companyName,
                                @"contact_user_name" : userName,
                                @"contact_phone" : phone,
                                @"consulting_problem" : problem,
                                @"reward_value" : rewardValue,
                                @"consulting_time" : time,
                                @"consulting_location" : location,          //区域
                                @"consulting_location_desc" : locationDesc, //地点
  };

  netCompletion_t netCompletion = ^(AFHTTPRequestOperation *operation, id responseObject) {
    NSDictionary *resultDict = responseObject;
    if (resultDict) {
      if (completion) {
        NSDictionary *resultObject = [resultDict objectForKey:DicKeyResult];
        NSInteger code = [[resultDict objectForKey:DicKeyErrorCode] integerValue];
        NSString *description = [resultDict objectForKey:DicKeyErrorMsg];
        description = description ? description : @"";
        completion(resultObject, code, description);
      }
    }
  };

  netFailure_t netFailure = ^(AFHTTPRequestOperation *option, NSError *e) {
    if (error) {
      error(e);
    }
  };

  AFHTTPRequestOperationManager *manager = [ApiBase shareInstance].shareManager;
  [manager POST:[url absoluteString] parameters:parameters success:netCompletion failure:netFailure];
}

@end
